package com.fmi.chesstern.strategy;

import java.util.ArrayList;

import com.fmi.chesstern.gameboard.GameBoard;
import com.fmi.chesstern.gameboard.GameField;
import com.fmi.chesstern.pieces.ChessPiece;
import com.fmi.chesstern.pieces.Pawn;
import com.fmi.chesstern.pieces.ChessPiece.PieceColor;

public class PawnStrategy implements IGameStrategy {

	public ArrayList<GameField> getValidMoves(GameField currentPosition, GameBoard board) {
		ArrayList<GameField> allMoves = new ArrayList<GameField>();
		Pawn pawn = (Pawn) board.getPieces().get(currentPosition.toString());
		if (pawn.getColor() == PieceColor.WHITE) {
			GameField frontPosition = new GameField(
					currentPosition.getRow() - 1, currentPosition.getColumn());
			if (board.getPieces().get(frontPosition.toString()) == null) {
				allMoves.add(frontPosition);
			}
			
			if (isPawnInStartPos(currentPosition.getRow(), pawn.getColor())) {
				GameField doubleFrontPosition = new GameField(
						currentPosition.getRow() - 2, currentPosition.getColumn());
				if (board.getPieces().get(doubleFrontPosition.toString()) == null 
						&& board.getPieces().get(frontPosition.toString()) == null) {
					allMoves.add(doubleFrontPosition);
				}
			}
			
			GameField rightPosition = new GameField(
					currentPosition.getRow() - 1, currentPosition.getColumn() + 1);
			ChessPiece piece = board.getPieces().get(rightPosition.toString());
			if (piece != null && piece.getColor() != pawn.getColor()) {
				allMoves.add(rightPosition);
			}
			
			GameField leftPosition = new GameField(
					currentPosition.getRow() - 1, currentPosition.getColumn() - 1);
			piece = board.getPieces().get(leftPosition.toString());
			if (piece != null && piece.getColor() != pawn.getColor()) {
				allMoves.add(leftPosition);
			}
		} else if (pawn.getColor() == PieceColor.BLACK) {
			GameField frontPosition = new GameField(
					currentPosition.getRow() + 1, currentPosition.getColumn());
			if (board.getPieces().get(frontPosition.toString()) == null) {
				allMoves.add(frontPosition);
			}
			
			if (isPawnInStartPos(currentPosition.getRow(), pawn.getColor())) {
				GameField doubleFrontPosition = new GameField(
						currentPosition.getRow() + 2, currentPosition.getColumn());
				if (board.getPieces().get(doubleFrontPosition.toString()) == null 
						&& board.getPieces().get(frontPosition.toString()) == null) {
					allMoves.add(doubleFrontPosition);
				}
			}
			
			GameField rightPosition = new GameField(
					currentPosition.getRow() + 1, currentPosition.getColumn() + 1);
			ChessPiece piece = board.getPieces().get(rightPosition.toString());
			if (piece != null && piece.getColor() != pawn.getColor()) {
				allMoves.add(rightPosition);
			}
			
			GameField leftPosition = new GameField(
					currentPosition.getRow() + 1, currentPosition.getColumn() - 1);
			piece = board.getPieces().get(leftPosition.toString());
			if (piece != null && piece.getColor() != pawn.getColor()) {
				allMoves.add(leftPosition);
			}
		}
		
		ArrayList<GameField> moves = new ArrayList<GameField>();
		for (GameField move : allMoves) {
			if (GameField.isFieldCorrect(move)) {
				moves.add(move);
			}
		}
		return moves;
	}

	private boolean isPawnInStartPos(int row, PieceColor color) {
		boolean startPos = false;
		if (color == PieceColor.WHITE) {
			startPos = (row == 6);
		} else if (color == PieceColor.BLACK) {
			startPos = (row == 1);
		}
		return startPos;
	}

}
